﻿<%@ WebHandler Language="C#" Class="VehicleImage" %>

using System;
using System.Web;
using System.Linq;

public class VehicleImage : IHttpHandler
{

    public int VehicleId
    {
        get
        {
            return Helper.QSIntValue("vid");
        }
    }

    public void ProcessRequest(HttpContext context)
    {

        try
        {
            DataContexts.Supplier.SupplierDataContext suppContext = new DataContexts.Supplier.SupplierDataContext();
            DataContexts.Supplier.SupplierVehicle vehicle = suppContext.SupplierVehicles.FirstOrDefault(p => p.SupplierVehicleId == VehicleId);

            if (vehicle != null)
            {

                byte[] data = vehicle.VehicleImage.ToArray();
                using (System.IO.Stream st = new System.IO.MemoryStream(data))
                {
                    context.Response.Clear();
                    context.Response.ClearHeaders();
                    context.Response.ClearContent();

                    context.Response.ContentType = vehicle.ImageType;  //Or other you need 
                    context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + vehicle.ImageName + "\"");

                    context.Response.BinaryWrite(data);
                }
            }
        }
        catch
        {
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}